package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.User;
import com.qf.pojo.UserTable;
import com.qf.service.USerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private USerService service;

    private String uploadFileName;

    @RequestMapping("findByPage")
    @ResponseBody
    public JsonResult findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "5") Integer pageSize)
    {

//        System.out.println("findByPage");

        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有
        List<UserTable> users = service.findAll();
        //封装到PageInfo对象
        PageInfo<UserTable> pageInfo = new PageInfo<>(users);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setData(pageInfo.getList());

        return jsonResult;
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        service.deleteAll(ids);

        return "success";
    }


    @RequestMapping("excel")
    @ResponseBody
    public String excel(Integer[] ids) {
        List<UserTable> users = service.excel(ids);

        //生成Excel表格
        String file = "D:\\1000phone\\IDEA Temp Project\\springMVC\\guimei\\excel"
                + "贵美"+System.currentTimeMillis() + ".xlsx";
        //获取当前文件对象
        File file1 = new File(file);
        //获取父目录文件对象
        File parentFile = file1.getParentFile();
        //判断文件对象父目录是否存在
        if (!parentFile.exists()) {
            //连同子目录一起创建
            parentFile.mkdir();
        }

        EasyExcel.write(file, UserTable.class).sheet("模板1").doWrite(users);

        return "success";
    }

    @RequestMapping("updateUser")
    @ResponseBody
    public String updateUser(UserTable userTable) {

        System.out.println(userTable);

        service.updateUser(userTable);

        return "success";
    }

    @RequestMapping("deleteUserById")
    @ResponseBody
    public String deleteUserById(Integer id) {

//        System.out.println(id);

        service.deleteById(id);

        return "success";
    }

    @RequestMapping("upload")
    @ResponseBody
        public JsonResult upload(MultipartFile file) {
        //上传的源文件名
        uploadFileName = file.getOriginalFilename();
        //使用uuid声明前缀，防止上传的文件名重复
        uploadFileName = UUID.randomUUID().toString() + uploadFileName;
        //声明上传的路劲
        String uploadPath = "D:\\Program Files\\tomcat-server\\apache-tomcat-8.5.31\\webapps\\guimei";

        try {
            //上传
            file.transferTo(new File(uploadPath, uploadFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8083/guimei/" + uploadFileName);

        return jsonResult;
    }

    @RequestMapping("addUser")
    public String addUser(UserTable userTable) {
        //设置用户图片地址
        userTable.setUserImage("http://localhost:8083/guimei/" + uploadFileName);

        System.out.println(userTable);

        //调用方法
        service.addUser(userTable);

        return "redirect:/index.html";
    }

}
